﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;

public partial class ProductMag : System.Web.UI.Page
{
    static string prevPage = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                prevPage = Request.UrlReferrer.ToString();
            }
            catch (System.Exception ex)
            {
                prevPage = Request.Url.AbsoluteUri;
            }

            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            string productCode = Request.QueryString["code"];
            if (String.IsNullOrEmpty(productCode))
                Response.Redirect("Default.aspx");

            magazinedproducts product = service.getMagazinedProductsByCode(productCode);
            lblDescription.Text = String.Format("Description: {0}", product.description);
            lblName.Text = product.name;
            lblPrice.Text = String.Format("Amount: {0}", product.amount.ToString());
        }
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        string productCode = Request.QueryString["code"];
        if (String.IsNullOrEmpty(productCode))
            Response.Redirect("Default.aspx");
        
        int resultAmount = 0;
        if(!int.TryParse(txtAmountToRemove.Text, out resultAmount))
            return;       

        service.removeProductFromMagazine(service.getMagazinedProductsByCode(productCode).id, resultAmount);
        Response.Redirect(Page.Request.Url.ToString(), true);
    }

    protected void btnChange_Click(object sender, EventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        string productCode = Request.QueryString["code"];
        if (String.IsNullOrEmpty(productCode))
            Response.Redirect("Default.aspx");

        int resultAmount = 0;
        if (!int.TryParse(txtAmountToRemove.Text, out resultAmount))
            return;

        service.changeMagazinedProductAmount(productCode, resultAmount);
        Response.Redirect(Page.Request.Url.ToString(), true);
    }
}